define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'shiftform'],
function($, validate) {
  return {
    
    init: function() {
      this.initFromloadplace();
     
    },
    
    initFromloadplace: function() {

      $(document).find('form').bt_validate();

    	$('.fromloadplace-search').koalaSearch({
    		time: 1000,
    		url: window.__CTX__.root + "/load-places/from-load-place-search",
    		value: ["id", "facilityCode", "facilityName"],
    		display: ["facilityCode", "facilityName"],
        ajaxData: function() {
          return {
            "key1": "124",
            "key2": $(".company").val()
          }
        }
    	});

    	$('.fromloadplace-search').on("koala.search.value.changed", function(e){
      	  console.log("123");
      	})
    	
        $('[data-target="#modal-search-fromloadplace"]').on('open.koala.search.modal', function(event) {
        
          event.preventDefault();
         /*Act on the event*/ 
        
          var url = window.__CTX__.root + "/load-places/from-load-place-model-search?" + 
                  $(".fromloadplace-search").attr("name") + "=" + $(".fromloadplace-search").val() +
                  "&" + $(".company").attr("name") + "=" + $(".company").val();

          var $this = $(event.currentTarget);
          var id = $this.attr('data-target').slice(1);


          $.get(url, function (data) {
            $('<div class="modal fade" id="' + id + '" role="dialog" aria-hidden="true">' + data + '</div>').modal().on('hidden.bs.modal', function (e) {
              return $(e.currentTarget).remove();
            }).on('shown.bs.modal', function () {
              return $this.trigger('shown.modal');
            });
          });

      });

      // $('.koala-search').koalaSearch.beforeSearch(function(){
      //   // 校验
      //   console.log("myself before search");
      // })
      
      $('select[name="company"]').select2({
        theme: "classic",
        placeholder : '请选择',
        minimumResultsForSearch : -1,
        allowClear : true,
      });
    },
    
    initToloadplace:function() {

        $(document).find('form').bt_validate();

      	$('.toloadplace-search').koalaSearch({
      		time: 1000,
      		url: window.__CTX__.root + "/facility-distances/to-load-place-search",
      		value: ["id", "code", "name"],
      		display: ["code", "name"],
          ajaxData: function() {
            return {
              "key1": "124",
              "key2": $(".company").val()
            }
          }
      	});
      	
    	$('.toloadplace-search').on("koala.search.value.changed", function(e){
      	  console.log("123");
      	})

        $('[data-target="#modal-search-toloadplace"]').on('open.koala.search.modal', function(event) {
          
          event.preventDefault();
           /*Act on the event */
          
          var url = window.__CTX__.root + "/facility-distances/to-load-place-model-search?" + 
                    $(".toloadplace-search").attr("name") + "=" + $(".toloadplace-search").val() +
                    "&" + $(".company").attr("name") + "=" + $(".company").val();

          var $this = $(event.currentTarget);
          var id = $this.attr('data-target').slice(1);


          $.get(url, function (data) {
            $('<div class="modal fade" id="' + id + '" role="dialog" aria-hidden="true">' + data + '</div>').modal().on('hidden.bs.modal', function (e) {
              return $(e.currentTarget).remove();
            }).on('shown.bs.modal', function () {
              return $this.trigger('shown.modal');
            });
          });

        });

        // $('.koala-search').koalaSearch.beforeSearch(function(){
        //   // 校验
        //   console.log("myself before search");
        // })
        
       
      },
    
    
  }
})